﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WC3Game
{
    public enum SlotStatus : byte { Unknown = 255, Open = 0, Closed, Occupied };
    public enum SlotRace : byte { Unknown = 255, Human = 1, Orc = 2, NightElf = 4, Undead = 8, Random = 32, Fixed = 64 }
    public enum SlotComp : byte { Unknown = 255, Easy = 0, Normal = 1, Hard = 2 };

    public class GameSlot
    {
        public byte id = 255;
        public byte downloadStatus = 255;
        private byte slotStatus = 255;
        public byte computer = 255;
        public byte team = 255;
        public byte color = 255;
        private byte race = 255;
        private byte computerType = 255;

        public SlotComp ComputerType
        {
            get { return (SlotComp)computerType; }
        }
        public byte handicap = 255;

        public SlotRace Race
        {
            get { return (SlotRace)race; }

        }

        public SlotStatus SlotStatus
        {
            get
            {
                return (SlotStatus)slotStatus;
            }
        }

        public GameSlot()
        {
        }

        const int slotDataStartPos = 7;

        public static int GetSlotInfoSize()
        {
            byte[] byteArray = command.Range(5, 7);
            int slotSize = 0;
            if (BitConverter.IsLittleEndian)
                byteArray = byteArray.Reverse().ToArray();

            slotSize = BitConverter.ToInt16(byteArray, 0);

            return slotSize;
        }

        static byte[] command = null;

        public static List<GameSlot> DecodeSlots(byte[] commandData)
        {
            command = commandData;

            List<GameSlot> gameSlotList = new List<GameSlot>();

            for (int slot = 0; slot < GetSlotInfoSize(); slot++)
            {
                int startPos = (slot * 9) + slotDataStartPos;
                int endPos = startPos + 9;

                byte[] slotDataArray = commandData.Range(startPos, endPos);

                GameSlot g = new GameSlot(slotDataArray);
                gameSlotList.Add(g);
            }

            return gameSlotList;
        }

        public GameSlot(byte[] slotData)
        {
            for (int i = 0; i < slotData.Length; i++)
            {
                switch (i)
                {
                    case 0:
                        id = slotData[i];
                        break;
                    case 1:
                        downloadStatus = slotData[i];
                        break;
                    case 2:
                        slotStatus = slotData[i];
                        break;
                    case 3:
                        computer = slotData[i];
                        break;
                    case 4:
                        team = slotData[i];
                        break;
                    case 5:
                        color = slotData[i];
                        break;
                    case 6:
                        race = slotData[i];
                        break;
                    case 7:
                        computerType = slotData[i];
                        break;
                    case 8:
                        handicap = slotData[i];
                        break;
                    default:
                        throw new Exception("Slot Info Wrong Size!");
                        break;
                }
            }
        }
    };

}
